package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.my.bean.Day;
import com.my.bean.ResultPage;
import com.my.bean.User;
import com.my.bean.VoPage;
import com.my.service.DayService;
import com.my.util.SessionUtil;
import com.my.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 日报 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2018-09-26
 */
@Controller
@RequestMapping("/day")
public class DayController {

    @Autowired
    DayService dayService;
    //创建日志Logger的对象
    Logger logger = Logger.getLogger(DayController.class);


    /**
     * 添加日报
     * @param param
     * @return
     */
    @RequestMapping("/insertDay")
    @ResponseBody
    public boolean insertDay(String param, HttpSession session){
        //使用反射吧json字符串转换成实体类对象
        Day day = JSONObject.parseObject(param,Day.class);
        //按照创建日期倒叙排序
        day.setCreateTime(new Date().getTime());
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        day.setAreaid(areaid);
        boolean flag = dayService.insert(day);
        String msg = flag ? "添加成功" + day : "添加失败";
        logger.info(msg);
        return flag;
    }



    @RequestMapping("/selectList")
    @ResponseBody
    public List<Day> selectList(String sendName,String logContent,String test6,HttpSession session ){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map=new HashMap();
        map.put("stra","");
        map.put("end","");
        map.put("sendName",sendName);
        if(!test6.equals("null")&&test6!=""&&test6!=null){
            //把空格换成空
            test6 = test6.replace(" ","");
            //街区字符串
            int i = StringUtils.jiequ(test6,"-",3);

            //开始时间
            String startDate = test6.substring(0,i);
            //结束时间
            String endDate =  test6.substring(i+1,test6.length());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date dateStart=new Date();
            Date datEnd=new Date();
            try {
                dateStart = format.parse(startDate);
                datEnd = format.parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //把开始时间日期转换成时间戳
            Long stra = dateStart.getTime();

            //把结束时间转换成时间戳
            Long end = datEnd.getTime();

            map.put("stra",stra);
            map.put("end",end);
        }
        map.put("logContent",logContent);
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        List<Day> monthlyReport= dayService.findDay(map,areaid,admin);
        return monthlyReport;







    }








    /**
     * 查询日报
     * @param lin
     * @param
     * @param voPage
     * @return
     */
    @RequestMapping("/selectDay")
    @ResponseBody
    public ResultPage<Day> selectDay(String lin,String data,VoPage voPage,HttpSession session ){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map = new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        ResultPage<Day> list = dayService.selectMyPage(map,voPage,areaid,admin);
        return list;


    }


    /**
     * 修改日报
     * @param param
     * @return
     */
    @RequestMapping("/updateDay")
    @ResponseBody
    public String updateDayy(String data1,String data2,String param){
        Day day = JSONObject.parseObject(param,Day.class);
        if(dayService.updateById(day)){
            return "修改成功";
        }else{
            return "修改失败";
        }

    }


    /**
     * 删除日报
     * @param id
     * @return
     */
    @RequestMapping("/deleteDay")
    @ResponseBody
    public boolean deleteDay(Long id){
        return dayService.deleteById(id);
    }


    /**
     * 返回日报
     * @return
     */
    @RequestMapping("/day")
    public String day(){
        return "day";
    }

    /**
     * 返回日报添加页面
     * @return
     */
    @RequestMapping("/day_add")
    public String dayAdd(){
        return "/Alert/day_add";
    }

    /**
     * 编辑日报
     * @return
     */
    @RequestMapping("/day_edit")
    public String dayEdit(){
        return "dat_edit";
    }


    /**
     * 返回看日报
     */
    @RequestMapping("/seeDay")
    public String seeDay(){
        return "seeDay";
    }








}
